package control
{
	import com.greensock.TweenLite;
	
	import events.MyEvent;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	public class DragMoveH extends EventDispatcher
	{
		protected const dragDistance:Number=10;//触摸结束时如果大于这个距离  判定为要移动过
		protected var parent:DisplayObjectContainer;
		protected var startX:Number;
		private var closed:Boolean=false;
		protected var startMouseX:Number;
		public var moveDisatance:Number;
		private var minPosition:Number;
		private var maxPosition:Number;
		public function DragMoveH(target:DisplayObjectContainer,left:Number,right:Number)
		{
			parent=target;
			minPosition=left;
			maxPosition=right;
			parent.stage.addEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
		}
		
		public function enable():void
		{
			parent.stage.addEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
		}
		
		public function disable():void
		{
			parent.stage.removeEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
		}
		
		public function get isClick():Boolean
		{
			return Math.abs(moveDisatance)<dragDistance;
		}
		
		protected function handleMouseDown(e:MouseEvent):void
		{
			parent.dispatchEvent(new MyEvent("moved",null,true));
			if(closed)return;
			startMouseX=e.stageX;
			startX=parent.x;
			parent.stage.addEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
			parent.root.addEventListener(Event.ENTER_FRAME,listenMousePosition);
		}
		
		private function listenMousePosition(e:Event):void
		{
			parent.dispatchEvent(new MyEvent("moved",null,true));
			var nowMouseX:Number=parent.root.mouseX;
			moveDisatance=nowMouseX-startMouseX;
			var targetX:Number=startX+moveDisatance;
			moveTarget(targetX);
		}
		
		protected function handleMouseUp(e:MouseEvent):void
		{
			if(closed)return;
			if(parent.stage)
			{
				parent.stage.removeEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
				parent.root.removeEventListener(Event.ENTER_FRAME,listenMousePosition);
			}
		}
		
		private function moveTarget(targetX:Number):void
		{
			if(targetX<minPosition)
			{
				targetX=minPosition;
			}
			else if(targetX>maxPosition)
			{
				targetX=maxPosition;
			}
			TweenLite.to(parent,0.5,{x:targetX,onComplete:back});
		}
		
		protected function back():void
		{
			var targetX:Number=parent.x;
			if(parent.x<minPosition)
			{
				targetX=minPosition;
			}
			else if(parent.x>maxPosition)
			{
				targetX=maxPosition;
			}
			TweenLite.to(parent,0.5,{x:targetX});
		}
		
		public function set move(distance:Number):void
		{
			startX=parent.x;
			moveDisatance=distance;
			var targetX:Number=startX+moveDisatance;
			moveTarget(targetX);
		}
		
		public function gc():void
		{
			closed=true;
			if(parent.stage.hasEventListener(MouseEvent.MOUSE_DOWN))
			{
				parent.stage.removeEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
				
			}
			if(parent.stage.hasEventListener(MouseEvent.MOUSE_UP))
			{
				parent.stage.removeEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
				
			}
			if(parent.root.hasEventListener(Event.ENTER_FRAME))
			{
				parent.root.removeEventListener(Event.ENTER_FRAME,listenMousePosition);
			}
			
		}
	}
}